﻿Imports System.Text.RegularExpressions

Public Class CINNERLabelState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-IN") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CINNERLabelState.DoExecute")
        'CFileLog.WriteLog("CINNERLabelState.DoExecute START---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
        Dim checkInfo As CCheck = CCheck.GetIns
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        'Dim waitTask As CPersonal = New CPersonal(pdaTask)
        Debug.WriteLine("CLabelCheckState.DoExecute...START")
        'MessageBox.Show("INNER_BARCD:" & task.ProcessText)

        Dim checkData As String = task.ProcessText
        'システム品番
        Dim sysitemCd As String = CBarcode.getBarcodeVl(checkData, CBarcode.GetBarcodeType(checkData), _
                                                            EnumBarcodeType.SYSITEM_CD)
        'ユニークコード
        Dim uniqueCd As String = CBarcode.getBarcodeVl(checkData, CBarcode.GetBarcodeType(checkData), _
                                                            EnumBarcodeType.UNIQUE_CD)
        '入り数
        Dim inBoxQty As Long = 0
        Dim hasuFlg As Boolean = False

        '端数の場合True
        '端数ラベルチェックロジック
        If CPersonal.HasuCheck(checkData) Then
            'エラーなった場合でまた読めないように
            Try
                '端数入力画面
                Dim prevWindowMode As EnumWindowMode
                Dim result As String = ""

                CCommon.FlagManage("READ_FLG", False)

                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                pdaTask.Hardware.alert()
                pdaTask.WindowManager.SetInputMessage(CConst.CNS_MSG_012, CConst.COLOR_BLACK, CConst.COLOR_GRAY)
                pdaTask.WindowManager.SetInputValue(0, CConst.COLOR_BLACK, CConst.COLOR_WHITE)
                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.INPUT_MODE)

                Do While inBoxQty = 0 'Not Isinstr(result, "[OK]", "[YES]", "[NO]")
                    Try
                        If inBoxQty = 0 Then
                            result = task.ReadTextWait(-1)
                            inBoxQty = CInt(result)
                        ElseIf inBoxQty > 0 Then

                            Exit Do
                        End If
                    Catch ex As Exception
                        inBoxQty = 0
                    End Try
                Loop
                hasuFlg = True
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
                CCommon.FlagManage("READ_FLG", True)

            Catch ex As Exception
            End Try
        Else
            '10.06.09.LEE START ============>>
            'バーコードーの数量部分に文字化けがあったらエラー
            Dim chkVl As String = CBarcode.getBarcodeVl(checkData, CBarcode.GetBarcodeType(checkData), EnumBarcodeType.INBOX_QTY)
            If Regex.IsMatch(chkVl, "[0-9A-Z]{2}") Then
                inBoxQty = CCommon.Change36(CBarcode.getBarcodeVl(checkData, CBarcode.GetBarcodeType(checkData), _
                                                                         EnumBarcodeType.INBOX_QTY))
            Else
                '文字化けでもう一同読み込んでください。というエラー
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_031, Color.White)
                task.ProcessText = ""
                task.ActionCd = "LABEL"
                nextState = GetNextState(task)
                'CFileLog.WriteLog("CINNERLabelState.DoExecute END---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
                Return nextState
            End If

            '10.06.09.LEE END ============>>
        End If

        '社内ラベル照合
        Dim labelDataList As CCheck.LabelData = New CCheck.LabelData

        labelDataList.readText = checkData
        labelDataList.uniqueCd = uniqueCd
        labelDataList.empCd = pdaTask.EmpCd
        labelDataList.isSend = False
        labelDataList.actionDateTime = Date.Now
        labelDataList.labelQty = inBoxQty      'ラベルにある入り数
        labelDataList.isFraction = hasuFlg '端数の場合はTrue以外はFalse

        'Dim InvoceOrder As CCheck.OrderData = checkInfo.GetOrderData(pdaTask.OrderCd)

        'ラベルリストにADD
        ' 0 -> 成功
        ' 1 -> 数量オーバーの場合
        ' 2 -> orderCdが存在しない
        ' 3 -> 重複するuniqueCdがある場合
        ' 4 -> 品番が違い場合
        'CFileLog.WriteLog("CINNER_ADDLABEL STR---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
        Select Case checkInfo.AddLabelData(pdaTask.OrderCd, sysitemCd, labelDataList)

            Case 0
                'CFileLog.WriteLog("CINNER_ADDLABEL END---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
                '成功
                pdaTask.Hardware.nextB()
                Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(pdaTask.OrderCd)
                setData.RemainedQty = CInt(setData.RemainedQty) - CInt(inBoxQty)
                setData.ActionQty += CInt(inBoxQty)
                setData.ActionBoxQty += 1
                setData.IsFraction = hasuFlg
                '  CFileLog.WriteLog("画面 値設定--END---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)

            Case 1
                Dim msgFlg As Boolean = AppManager.m_AppSetting.OverCountMsg
                If Not CCommon.FlagManage("overCount") Then

                    pdaTask.Hardware.nextB()

                    Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(pdaTask.OrderCd)
                    setData.RemainedQty = CInt(setData.RemainedQty) - CInt(inBoxQty)
                    setData.ActionQty += CInt(inBoxQty)
                    setData.ActionBoxQty += 1
                    'setData.OrderCd = InvoceOrder.invoiceCd
                    setData.IsFraction = hasuFlg
                Else
                    'エラー表示
                    If CCommon.FlagManage("overCount") Then

                        If msgFlg Then
                            '数量オーバーの場合
                            If CPersonal.ConfirmWindow(pdaTask, EnumWindowMode.YESNO_MODE, _
                                                         Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_019, Color.White) Then
                                pdaTask.Hardware.nextB()
                                CCommon.FlagManage("overCount", False)

                                Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(pdaTask.OrderCd)
                                setData.RemainedQty = CInt(setData.RemainedQty) - CInt(inBoxQty)
                                setData.ActionQty += CInt(inBoxQty)
                                setData.ActionBoxQty += 1
                                'setData.OrderCd = InvoceOrder.invoiceCd
                                setData.IsFraction = hasuFlg

                            Else
                                checkInfo.DelLabelData(task.ProcessText)

                            End If
                        Else

                            pdaTask.Hardware.nextB()

                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(pdaTask.OrderCd)
                            setData.RemainedQty = CInt(setData.RemainedQty) - CInt(inBoxQty)
                            setData.ActionQty += CInt(inBoxQty)
                            setData.ActionBoxQty += 1
                            'setData.OrderCd = InvoceOrder.invoiceCd
                            setData.IsFraction = hasuFlg

                        End If


                    End If

                End If

            Case 2
                'orderCdが存在しない
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_013, Color.White)
            Case 3
                '重複するuniqueCdがある場合
                Dim uniqueData As CCheck.UniqueCdData = checkInfo.ChkUniqueCdData(labelDataList.uniqueCd)
                Dim readYear As String = uniqueData.actionDateTime.ToString("yy")
                Dim readMonth As String = uniqueData.actionDateTime.ToString("MM")
                Dim readDay As String = uniqueData.actionDateTime.ToString("dd")
                Dim empInfo As CCheck.EmpData = checkInfo.ChkEmpData(uniqueData.empCd)
                '統括コード
                Dim errMsg As String = CCommon.msgFormat(CConst.CNS_MSG_018, readYear, readMonth, readDay, empInfo.empName)
                ' 重複するuniqueCdがある場合
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, errMsg, Color.White)
            Case 4
                '品番が違い場合
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_017, Color.White)

        End Select

        task.ProcessText = ""
        task.ActionCd = "LABEL"
        nextState = GetNextState(task)
        'CFileLog.WriteLog("CINNERLabelState.DoExecute END---", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.READ)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState.DoExit")
        Return True
    End Function


End Class
